package com.tuanzi.loan.business.repository.sms.impl;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.constants.sms.SmsUserType;
import com.tuanzi.loan.business.repository.sms.custom.LoanMobnumApiScoreCustomRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.family.sms.ExternalSmsCalculateCondition;
import com.tuanzi.loan.vo.response.family.sms.LoanMobnumApiScoreResponse;
import com.tuanzi.server.loan_base.util.DateUtils;
import org.jooq.Record;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;

import java.util.List;
import java.util.stream.Collectors;

import static com.tuanzi.loan.generated.jooq.loan.tables.MobnumApiScore.MOBNUM_API_SCORE;

/**
 * @Author: chenweijun
 * @Date: 2018/10/31 19:30
 * @Description:
 */
public class LoanMobnumApiScoreRepositoryImpl extends JOOQBaseRepository implements LoanMobnumApiScoreCustomRepository {

    @Override
    protected TableImpl getTable() {
        return MOBNUM_API_SCORE;
    }

    @Override
    public List<LoanMobnumApiScoreResponse> list(ExternalSmsCalculateCondition condition) {


        List<Object> parameters = Lists.newArrayList();

        StringBuilder sb = new StringBuilder();
        sb.append("SELECT DISTINCT(TB_MAS.`mobnum`) AS mobnum, TB_MAS.id, TB_MAS.export_count AS exportCount FROM `mobnum_api_score` AS TB_MAS ")
                .append(" LEFT JOIN `phone_location` TB_PL ON LEFT (TB_MAS.mobnum, 7) = TB_PL.`phone` WHERE 1=1");

        if(condition.getTongDunStartTime() != null){
            sb.append(" AND `tongdun_calculating_time` >= ?");
            parameters.add(DateUtils.formatDateToYyyyMMdd(condition.getTongDunStartTime()));
        }

        if(condition.getTongDunEndTime() != null){
            sb.append(" AND `tongdun_calculating_time` <= ?");
            parameters.add(DateUtils.formatDateToYyyyMMdd(condition.getTongDunEndTime()) + " 23:59:59");
        }

        if(condition.getTongDunMinScore() != null){
            sb.append(" AND `tongdun_intent_score` >= ?");
            parameters.add(condition.getTongDunMinScore());
        }

        if(condition.getTongDunMaxScore() != null){
            sb.append(" AND `tongdun_intent_score` <= ?");
            parameters.add(condition.getTongDunMaxScore());
        }

        if(condition.getUserTypes().contains(SmsUserType.REGISTERED_USER) && condition.getUserTypes().contains(SmsUserType.CHECKIN_BUT_NOT_REGISTER_USER)){
            sb.append(" AND (TB_MAS.`has_zhuce` = 1 OR (TB_MAS.`has_dengji` = 1 AND TB_MAS.`has_zhuce` =0)) ");
        }else if(condition.getUserTypes().contains(SmsUserType.REGISTERED_USER)){
            sb.append(" AND TB_MAS.`has_zhuce` = 1");
        }else if(condition.getUserTypes().contains(SmsUserType.CHECKIN_BUT_NOT_REGISTER_USER)){
            sb.append(" AND TB_MAS.`has_dengji` = 0 AND TB_MAS.`has_zhuce` = 0");
        }

        sb.append(" AND TB_PL.`province` IN (?)");
        parameters.add(DSL.list(condition.getProvinces().stream().map(DSL::val).collect(Collectors.toList())));

        sb.append(" AND TB_PL.`isp` IN(?)");
        parameters.add(DSL.list(condition.getTelecomOperators().stream().map(DSL::val).collect(Collectors.toList())));

        return dslContext.fetch(sb.toString(), parameters.toArray()).map((Record record) ->{
            LoanMobnumApiScoreResponse response = new LoanMobnumApiScoreResponse();
            response.setId(record.get("id", Long.class));
            response.setMobnum(record.get("mobnum", String.class));
            response.setExportCount(record.get("exportCount", Integer.class));
            return response;
        });

    }

}
